package com.version.manager.util;

import com.version.manager.model.TreeNode;

import java.util.ArrayList;
import java.util.List;

public class TreeUtil {
    public static <T extends TreeNode> List<T> build(List<T> treeNodes,Object root){
        List<T> trees = new ArrayList<T>();
        for (T treeNode : treeNodes) {
            if (root.equals(treeNode.getParentId())) {
                trees.add(treeNode);
            }
            for (T it : treeNodes) {
                if (it.getParentId() == treeNode.getId()) {
                    if (treeNode.getChildren() == null) {
                        treeNode.setChildren(new ArrayList<TreeNode>());
                    }
                    treeNode.add(it);
                }
            }
        }
        return trees;
    }
}
